package com.akcome.apollo.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.akcome.apollo.domain.BmpNotice;
import com.akcome.apollo.repository.BmpNoticeRepository;
import com.akcome.apollo.service.BmpNoticeService;


/**
 * 通知记录Service接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class BmpNoticeServiceImpl implements BmpNoticeService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BmpNoticeRepository bmpNoticeRepository;
	
	@Override
	public BmpNotice save(BmpNotice bmpNotice){
		return bmpNoticeRepository.save(bmpNotice);
	}
	
	@Override
	public BmpNotice findOne(Long id){
		return bmpNoticeRepository.findOne(id);
	}
	
	@Override
	public Page<BmpNotice> findByPage(final Long accountId,Pageable page){
		return bmpNoticeRepository.findAll(new Specification<BmpNotice>() {
			
			@Override
			public Predicate toPredicate(Root<BmpNotice> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> params = new ArrayList<Predicate>();
				if(null!=accountId){
					params.add(criteriaBuilder.equal(root.get("accountId"), accountId));
				}
				criteriaQuery.orderBy(criteriaBuilder.desc(root.get("id")));
                Predicate[] predicates = new Predicate[params.size()];
                criteriaQuery.where(params.toArray(predicates));
				
				return null;
			}
		}, page);
	}
}
